package com.tom.dto;

import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * 返回消息
 *
 * @author Administrator
 */
@Getter
@Setter
public class MessageRes extends HashMap<Integer, List<String>> {

    private static final long serialVersionUID = -7019158293453291571L;

    // 正常状态码
    public final static Integer success_code = 10000;
    // 异常状态码
    public final static Integer error_code = 99999;

    private MessageRes(Integer code, List<String> msg) {
        this.put(code, msg);
    }

    public static MessageRes success() {
        return new MessageRes(success_code, new ArrayList<String>(Arrays.asList("success")));
    }

    public MessageRes addMsg(String msg) {
        if (success_code.equals(getCode())) {
            clear();
            put(error_code, new ArrayList<String>());
        }
        getMsg().add(msg);
        return this;
    }

    public MessageRes addSuccessMsg(String msg) {
        getMsg().add(msg);
        return this;
    }
    public List<String> getMsg() {
        return this.get(getCode());
    }

    public Integer getCode() {
        return this.keySet().iterator().next();
    }

}
